package com.j3.db;

public enum ServerType {
	
	MYSQL(
			0, 
			"com.mysql.jdbc.Driver", 
			"jdbc:mysql://#IP#:#PORT#/#DBNAME#",
			"select 1"),
			
	MSSQL(
			1, 
			"net.sourceforge.jtds.jdbc.Driver", 
			"jdbc:jtds:sqlserver://#IP#:#PORT#/#DBNAME#",
			"select 1"),
			
	ORACLE(
			2, 
			"oracle.jdbc.driver.OracleDriver", 
			"jdbc:oracle:thin:@#IP#:#PORT#:#DBNAME#",
			"select 1 from dual"
			);
	
	private final int typeId;
	private final String driverClass;
	private final String driverUrl;
	private final String validationQuery;
	
	ServerType(int typeId, String driverClass, String driverUrl, String validationQuery) {
		this.typeId = typeId;
		this.driverClass = driverClass;
		this.driverUrl = driverUrl;
		this.validationQuery = validationQuery;
	}
	
	public String getDriverUrl(Db db) {
		return driverUrl.replace("#IP#", db.getIp())
									.replace("#PORT#", String.valueOf(db.getPort()))
									.replace("#DBNAME#", db.getDbName());
	}
	
	public boolean isMysql() {
		return typeId == MYSQL.typeId;
	}
	
	public int getTypeId() {
		return typeId;
	}
	
	public String getDriverClass() {
		return driverClass;
	}
	
	public String getDriverUrl() {
		return driverUrl;
	}
	
	public String getValidationQuery() {
		return validationQuery;
	}
	public static ServerType getServerType(int typeId) {
		ServerType[] values = ServerType.values();
		if(typeId < values.length && typeId >= 0) {
			return values[typeId];			
		}
		return null;
	}

}
